package src;

import hrSensor.HRSensor;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import sensors.HeartSensor;
import sensors.ISensor;

/**
 * Comunicatore visivo JFrame che presenta un allarme testuale
 * 
 * @author a.gallo
 *
 */

public class JFrameCommunicator implements ICommunicator {

	ArrayList<ISensor> sensori;
	JLabel outputLabel;
	JLabel heartTXT;
	JLabel heartIMG;
	JTextArea txtArea = new JTextArea(6, 20);
	GridBagConstraints c;
	int prec = 60;

	public JFrameCommunicator(ArrayList<ISensor> sensori) {

		CDefinitor def = new CDefinitor();

		this.sensori = sensori;
		JFrame frame = new JFrame("VisualCommunicator");

		JPanel panel = new JPanel(new GridBagLayout());

		JLabel tmp = new JLabel("Stato: ");
		panel.add(tmp, def.cTXTLabel);

		outputLabel = new JLabel("BEEP?");
		panel.add(outputLabel, def.cTXTOutput);

		txtArea.setText("");
		txtArea.setEditable(false);
		txtArea.setFocusable(false);
		panel.add(txtArea, def.cTXTArea);

		c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c.gridy = 1;
		heartIMG = new JLabel();
		heartIMG.setIcon(new ImageIcon(HRSensor.getImage(60)));
		heartIMG.setSize(80, 80);
		panel.add(heartIMG, def.cHeart);

		c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 2;
		c.gridy = 2;
		c.ipadx = 40;
		c.ipady = 40;
		heartTXT = new JLabel();
		heartTXT.setText("60");
		heartTXT.setForeground(new Color(60, 179, 113));
		heartTXT.setFont(new Font(heartTXT.getFont().getName(), Font.PLAIN, 60));
		panel.add(heartTXT, def.cHeartPulses);

		panel.setBackground(Color.white);
		frame.add(panel);

		frame.setSize(400, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	@Override
	public void beep() {

		outputLabel.setForeground(Color.RED);
		outputLabel.setText("SVEGLIATI!");
		txtArea.setText("");
		for (int i = 0; i < sensori.size(); i++) {
			txtArea.append(sensori.get(i).getClass().getSimpleName() + " => "
					+ sensori.get(i).getValue() + " Range: "
					+ sensori.get(i).getRange()[0] + " : "
					+ sensori.get(i).getRange()[1] + " : "
					+ sensori.get(i).getRange()[2] + "\n");
			if (sensori.get(i).getClass() == HeartSensor.class) {
				int pulses = (int) sensori.get(i).getValue();
				if (Math.abs(pulses-prec)>10) {
					heartIMG.setIcon(new ImageIcon(HRSensor.getImage(pulses)));
					prec = pulses;
				}
				heartTXT.setText(pulses + "");

			}

		}

	}

	@Override
	public void unbeep() {
		outputLabel.setForeground(Color.BLACK);
		outputLabel.setText("Sveglio.");
		txtArea.setText("");
		for (int i = 0; i < sensori.size(); i++) {
			txtArea.append(sensori.get(i).getClass().getSimpleName() + " => "
					+ sensori.get(i).getValue() + " Range: "
					+ sensori.get(i).getRange()[0] + " : "
					+ sensori.get(i).getRange()[1] + " : "
					+ sensori.get(i).getRange()[2] + "\n");
			if (sensori.get(i).getClass() == HeartSensor.class) {
				int pulses = (int) sensori.get(i).getValue();
				if (Math.abs(pulses-prec)>10) {
					heartIMG.setIcon(new ImageIcon(HRSensor.getImage(pulses)));
					prec = pulses;
				}
				heartTXT.setText(pulses + "");

			}
		}

	}

}
